{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "def move_files_to_target_dir(src_dir, target_dir):\n",
    "    \"\"\"\n",
    "    将 src_dir 及其所有子目录中的所有文件移动到 target_dir。\n",
    "    \n",
    "    参数：\n",
    "    src_dir (str): 源目录，所有文件将从该目录及其子目录中移动。\n",
    "    target_dir (str): 目标目录，所有文件将被移动到此目录下。\n",
    "    \"\"\"\n",
    "    # 确保目标目录存在\n",
    "    if not os.path.exists(target_dir):\n",
    "        os.makedirs(target_dir)\n",
    "    \n",
    "    # 遍历 src_dir 及其所有子目录\n",
    "    for root, _, files in os.walk(src_dir):\n",
    "        for file in files:\n",
    "            # 构建文件的完整路径\n",
    "            file_path = os.path.join(root, file)\n",
    "            \n",
    "            # 构建目标路径\n",
    "            target_path = os.path.join(target_dir, file)\n",
    "            \n",
    "            # 如果目标路径的文件已存在，则覆盖\n",
    "            if os.path.exists(target_path):\n",
    "                print(f\"覆盖文件：{target_path}\")\n",
    "                os.remove(target_path)\n",
    "            \n",
    "            # 移动文件到目标目录\n",
    "            shutil.move(file_path, target_path)\n",
    "            print(f\"移动文件：{file_path} -> {target_path}\")\n",
    "\n",
    "# 设置目录\n",
    "current_dir = ''  # 源目录\n",
    "target_dir = '' # 目标目录\n",
    "move_files_to_target_dir(current_dir, target_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 根据关键字移动或删除文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "def move_files_with_B0(source_dir, target_dir=None, action='move'):\n",
    "    \"\"\"\n",
    "    移出所有文件名中包含'B0'的文件\n",
    "    \n",
    "    参数:\n",
    "        source_dir: 源目录路径\n",
    "        target_dir: 目标目录路径（如果是移动操作）\n",
    "        action: 'move' 或 'delete'，决定是移动还是删除文件\n",
    "    \"\"\"\n",
    "    if not os.path.exists(source_dir):\n",
    "        print(f\"源目录不存在: {source_dir}\")\n",
    "        return\n",
    "    \n",
    "    if action == 'move' and target_dir is None:\n",
    "        print(\"移动操作需要指定目标目录\")\n",
    "        return\n",
    "    \n",
    "    if action == 'move' and not os.path.exists(target_dir):\n",
    "        os.makedirs(target_dir)\n",
    "    \n",
    "    moved_count = 0\n",
    "    deleted_count = 0\n",
    "    \n",
    "    for filename in os.listdir(source_dir):\n",
    "        if 'B0' in filename:\n",
    "            source_path = os.path.join(source_dir, filename)\n",
    "            \n",
    "            if action == 'move':\n",
    "                target_path = os.path.join(target_dir, filename)\n",
    "                shutil.move(source_path, target_path)\n",
    "                moved_count += 1\n",
    "                print(f\"已移动: {filename} -> {target_dir}\")\n",
    "            elif action == 'delete':\n",
    "                os.remove(source_path)\n",
    "                deleted_count += 1\n",
    "                print(f\"已删除: {filename}\")\n",
    "    \n",
    "    if action == 'move':\n",
    "        print(f\"\\n操作完成。共移动了 {moved_count} 个文件到 {target_dir}\")\n",
    "    else:\n",
    "        print(f\"\\n操作完成。共删除了 {deleted_count} 个文件\")\n",
    "\n",
    "# 使用示例\n",
    "if __name__ == \"__main__\":\n",
    "    source_directory = input(\"请输入源目录路径: \")\n",
    "    \n",
    "    action_choice = input(\"选择操作类型 (1-移动, 2-删除): \")\n",
    "    if action_choice == '1':\n",
    "        target_directory = input(\"请输入目标目录路径: \")\n",
    "        move_files_with_B0(source_directory, target_directory, 'move')\n",
    "    elif action_choice == '2':\n",
    "        confirm = input(\"确定要删除所有包含'B0'的文件吗？(y/n): \")\n",
    "        if confirm.lower() == 'y':\n",
    "            move_files_with_B0(source_directory, action='delete')\n",
    "    else:\n",
    "        print(\"无效的选择\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
